
#import <UIKit/UIKit.h>
#import "GetPhotos.h"


@interface GetPhotos()<UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
    
}

@property (nonatomic, strong) UIViewController* ownerController;
@property (nonatomic, strong) UIImageView* myImageView;
@property (nonatomic, strong) UIImagePickerController *imagepicker;
@property (nonatomic, strong) UIPopoverController* popoverController;

@end


@implementation GetPhotos

@synthesize ownerController;
@synthesize myImageView;
@synthesize imagepicker;
@synthesize tag;
@synthesize delegate;
@synthesize popoverController = _popoverController;


-(void) initWithController:(UIViewController*) controller {
    self.ownerController = controller;
}


-(void) getPhotosFromCamera {
    self.imagepicker = [[UIImagePickerController alloc] init];
    
    self.imagepicker.delegate = self;  
    self.imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;  
    self.imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;  
    self.imagepicker.allowsEditing = YES;  
    [self.ownerController presentModalViewController:self.imagepicker animated:YES];
}


-(void) getPhotosFromPhotoLibiary {
    self.imagepicker = [[UIImagePickerController alloc] init];
    
    self.imagepicker.delegate = self;  
    self.imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
    self.imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;  
    self.imagepicker.allowsEditing = YES;  
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:self.imagepicker];
    self.popoverController = popover;
    //popoverController.delegate = self;
    
    [self.popoverController presentPopoverFromRect:CGRectMake(0, 0, 500, 500) inView: self.ownerController.view
                          permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    //[self.ownerController presentModalViewController:self.imagepicker animated:YES];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    
    //CGSize tempsize = [img size];
    self.myImageView = [[UIImageView alloc] initWithImage:img];
    
    [self.imagepicker dismissModalViewControllerAnimated:YES];
    self.myImageView.userInteractionEnabled=YES;
    //    CGRect imageFrame = self.myImageView.frame;
    //    CGPoint imageCenter = self.myImageView.center;
    //    UIImage *croppedImage;
    //    NSMutableDictionary *imageDescriptor = [editInfo mutableCopy];
    // CGFloat scaleSize = 400.0f;
    
    [self.myImageView setImage:img];
    [self.myImageView setFrame:CGRectMake(10, 10, 500, 500)];
    
    SEL sizeRecive = @selector(getPhotoFinished:);
    if (self.delegate && [self.delegate respondsToSelector:sizeRecive]) { 
        [self.myImageView setTag:self.tag];
        [self.delegate getPhotoFinished:img];
    }
    
//    if (self.delegate && [self.delegate respondsToSelector:getPhotoFinished:]) { 
//        //  [self.delegate performSelector:loadFailed withObject:self ];
//        [self.delegate getPhotoFinished:self.myImageView];
//    }
    
    return;
}




@end
